package com.mxfcode.back.controller;

import com.mxfcode.annotation.LoginRequired;
import com.mxfcode.back.service.*;
import com.mxfcode.bbs_common.util.StringUtils;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 后台页面跳转控制器
 * @author moxiufeng
 * @date 2021/2/25 22:00
 */
@Controller
@RequestMapping("/back")
@LoginRequired
public class BackController {

    private static final Logger logger = LoggerFactory.getLogger(BackController.class);

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BackPlateService backPlateService;
    @Autowired
    private BackArticleService backArticleService;
    @Autowired
    private BackAnswerQuestionsService answerQuestionsService;
    @Autowired
    private BackResourceService backResourceService;
    @Autowired
    private HostHolder hostHolder;
    /**
     * 跳转首页
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String toBackIndexPage(){
        return "/back/index";
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/main",method = RequestMethod.GET)
    public String toBackConsolePage(){
        return "/back/main";
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/set-basic",method = RequestMethod.GET)
    public String toBackSetBasicgPage(){
        return "/back/set-basic";
    }

    /**
     * 跳转文章类型界面（用户自定义）
     * @return
     */
    @RequestMapping(value = "/article_category-list",method = RequestMethod.GET)
    public String toBackArticleCategoryListPage(){
        return "/back/article_category-list";
    }

    @RequestMapping(value = "/answer-questions-list",method = RequestMethod.GET)
    public String toBackAnswerQuestionsListPage(){
        return "/back/answer-questions-list";
    }

    @RequestMapping(value = "/resource-list",method = RequestMethod.GET)
    public String toBackResourceListListPage(){
        return "/back/resource-list";
    }

    @RequestMapping(value = "/article_category_operation",method = RequestMethod.GET)
    public String toArticleCategoryOperation(String id, Model model){
        BBSCategory category = null;
        if(StringUtils.isNotBlank(id)){
            category = categoryService.getCategoryById(Integer.valueOf(id));
        }
        model.addAttribute("category",category);
        return "/back/article_category_operation";
    }
    @RequestMapping(value = "/answer_questions_operation",method = RequestMethod.GET)
    public String toAnswerQuestionsOperation(String id, Model model){
        BBSAnswerQuestions answerQuestions = null;
        if(StringUtils.isNotBlank(id)){
            answerQuestions = answerQuestionsService.getAnswerQuestionsById(Integer.valueOf(id));
        }
        model.addAttribute("answerQuestions",answerQuestions);
        return "/back/answer_questions_operation";
    }


    @RequestMapping(value = "/plate_operation",method = RequestMethod.GET)
    public String toPlateOperation(String id, Model model){
        BBSPlate plate = null;
        if(StringUtils.isNotBlank(id)){
            plate = backPlateService.getPlateById(id);
        }
        model.addAttribute("plate",plate);
        return "/back/plate_operation";
    }

    @RequestMapping(value = "/article_operation",method = RequestMethod.GET)
    public String toArticleOperation(String id, Model model){
        BBSArticle article = null;
        if(StringUtils.isNotBlank(id)){
            article = backArticleService.getArticleById(id);
        }
        model.addAttribute("article",article);
        return "/back/article_operation";
    }

    @RequestMapping(value = "/permission_list",method = RequestMethod.GET)
    public String toPermissionListPage(){
        return "/back/permission_list";
    }

    @GetMapping("/role_list")
    public String toRoleListPage() {
        logger.info("===进入角色管理===");
        return "/back/role_list";
    }


    @GetMapping("/article-list")
    public String toArticleListPage() {
        logger.info("查看文章列表");
        return "/back/article-list";
    }

    @GetMapping("/user_list")
    public String toUserListPage() {
        logger.info("===进入用户管理===");
        return "/back/user_list";
    }

    @GetMapping("/plate_list")
    public String toPlateListPage() {
        logger.info("进入网站管理 ===> 板块列表");
        return "/back/plate_list";
    }

    @GetMapping("/resource_operation")
    public String toResourceOperationPage(String id,Model model) {
        BBSResource resource = null;
        if(id != null){
            resource = backResourceService.getResourceById(Integer.valueOf(id));
        }
        model.addAttribute("resource",resource);
        return "/back/resource_operation";
    }
    @GetMapping("/personal-information")
    public String toPersonalInformation(Model model){
        model.addAttribute("user",hostHolder.getUser());
        logger.info("进入个人信息管理模块");
        return "/back/person-info";
    }
}
